/**
 * Copyright ©2003~2019年5月6日 szkingdom
 * @author raohj
 * @date 2019年5月6日
 * @file RAtomic.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年5月6日 由 raohj 新建文件
 */
package com.ccnu.demojava.atomic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.*;

/**
 * @author raohj
 * @date 2019年5月6日
 * @version
 * @comment to add your comment
 */
public class RAtomic {
	private static final Logger log = LoggerFactory.getLogger(RAtomic.class);

	// 基本类型——原子操作
	private static AtomicBoolean atomicB = new AtomicBoolean(true);
	private static AtomicInteger atomicI = new AtomicInteger(0);
	private static AtomicLong atomicL = new AtomicLong(0);

	// 数组——原子操作
	private static AtomicIntegerArray atomicIA = new AtomicIntegerArray(10);
	private static AtomicReferenceArray<RAtomicSubclass> atomicRA = new AtomicReferenceArray<>(10);

	// 引用类型——原子操作
	private static AtomicReference<RAtomicSubclass> atomicR = new AtomicReference<>();
	private static AtomicMarkableReference<RAtomicSubclass> atomicMR = new AtomicMarkableReference<RAtomicSubclass>(
			null, false);
	private static AtomicReferenceFieldUpdater<RAtomicSubclass, String> atomicRFU = new AtomicReferenceFieldUpdater<RAtomicSubclass, String>(){

		@Override
		public boolean compareAndSet(RAtomicSubclass arg0, String arg1, String arg2) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public String get(RAtomicSubclass arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void lazySet(RAtomicSubclass arg0, String arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void set(RAtomicSubclass arg0, String arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean weakCompareAndSet(RAtomicSubclass arg0, String arg1, String arg2) {
			// TODO Auto-generated method stub
			return false;
		}
		
	};
	
	// 字段——原子更新
	private static AtomicIntegerFieldUpdater<RAtomicSubclass> atomicIFU = new AtomicIntegerFieldUpdater<RAtomic.RAtomicSubclass>() {
		
		@Override
		public boolean weakCompareAndSet(RAtomicSubclass arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public void set(RAtomicSubclass arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void lazySet(RAtomicSubclass arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public int get(RAtomicSubclass arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public boolean compareAndSet(RAtomicSubclass arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			return false;
		}
	};

	public static class RAtomicSubclass {
		private String name;
		private int age;
	}

	public static class RAtomicRunnable implements Runnable {
//		private static AtomicInteger tickets = new AtomicInteger(100);
		private static AtomicLong tickets = new AtomicLong(100);

		@Override
		public void run() {
			while (tickets.get() > 0) {
				log.info("[" + Thread.currentThread().threadId() + "] sells:	" + (tickets.decrementAndGet() + 1));
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * @author raohj
	 * @date 2019年5月6日
	 * @comment 测试JDK自带的原子操作
	 * @param
	 * @return
	 */
	public static void test() {
		ExecutorService es = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 10; i++) {
			es.execute(new RAtomicRunnable());
		}
		es.shutdown();
	}
}
